home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htstools.c < prev    next >
C/C++ Source or Header  |  2006-08-15  |  30KB  |  1,109 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       various tools (filename analyzing ..)                  */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. /* String */
  42. #include <ctype.h>
  43. #include "htscore.h"
  44. #include "htstools.h"
  45. #include "htsstrings.h"
  46. #ifdef _WIN32
  47. #include "windows.h"
  48. #else
  49. #include <dirent.h>
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. #include <sys/stat.h>
  54. #endif
  55.  
  56. // Portable directory find functions
  57. #ifndef HTS_DEF_FWSTRUCT_find_handle_struct
  58. #define HTS_DEF_FWSTRUCT_find_handle_struct
  59. typedef struct find_handle_struct find_handle_struct;
  60. #endif
  61. #ifdef _WIN32
  62. struct find_handle_struct {
  63.   WIN32_FIND_DATAA hdata;
  64.   HANDLE handle;
  65. };
  66. #else
  67. struct find_handle_struct {
  68.   DIR * hdir;
  69.   struct dirent* dirp;
  70.   struct stat filestat;
  71.   char path[2048];
  72. };
  73. #endif
  74. #ifndef HTS_DEF_FWSTRUCT_topindex_chain
  75. #define HTS_DEF_FWSTRUCT_topindex_chain
  76. typedef struct topindex_chain topindex_chain;
  77. #endif
  78. struct topindex_chain {
  79.   int level;                          /* sort level */
  80.   char* category;                     /* category */
  81.   char name[2048];                    /* path */
  82.   struct topindex_chain* next;        /* next element */
  83. };
  84.  
  85.  
  86. /* Tools */
  87.  
  88. static int ehexh(char c) {
  89.   if ((c>='0') && (c<='9')) return c-'0';
  90.   if ((c>='a') && (c<='f')) c-=('a'-'A');
  91.   if ((c>='A') && (c<='F')) return (c-'A'+10);
  92.   return 0;
  93. }
  94.  
  95. static int ehex(char* s) {
  96.   return 16*ehexh(*s)+ehexh(*(s+1));
  97. }
  98.  
  99. static void unescapehttp(char* s, String* tempo) {
  100.   int i;
  101.   for (i=0;i<(int) strlen(s);i++) {
  102.     if (s[i]=='%' && s[i+1]=='%') {
  103.       i++;
  104.       StringAddchar(*tempo, '%');
  105.     } else if (s[i]=='%') {
  106.       char hc;
  107.       i++;
  108.       hc = (char) ehex(s+i);
  109.       StringAddchar(*tempo, (char) hc);
  110.       i++;    // sauter 2 caractΦres finalement
  111.     }
  112.     else if (s[i]=='+') {
  113.       StringAddchar(*tempo, ' ');
  114.     }
  115.     else
  116.       StringAddchar(*tempo, s[i]);
  117.   }
  118. }
  119.  
  120. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  121. // [adr et fil sont des buffers de 1ko]
  122. // 0 : ok
  123. // -1 : erreur
  124. // -2 : protocole non supportΘ (ftp)
  125. int ident_url_relatif(const char *lien,const char* origin_adr,const char* origin_fil,char* adr,char* fil) {
  126.   int ok=0;
  127.   int scheme=0;
  128.  
  129.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  130.  
  131.   // lien non vide!
  132.   if (strnotempty(lien)==0) return -1;    // erreur!
  133.  
  134.   // Scheme?
  135.   {
  136.     const char* a=lien;
  137.     while (isalpha((unsigned char)*a))
  138.       a++;
  139.     if (*a == ':')
  140.       scheme=1;
  141.   }
  142.  
  143.   // filtrer les parazites (mailto & cie)
  144.   // scheme+authority (//)
  145.   if (
  146.                (strfield(lien,"http://"))        // scheme+//
  147.             || (strfield(lien,"file://"))   // scheme+//
  148.             || (strncmp(lien,"//",2)==0)    // // sans scheme (-> default)
  149.        ) {
  150.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  151.       ok=-1;    // erreur URL
  152.     }
  153.   }
  154.   else if (strfield(lien,"ftp://")) {
  155.     // Note: ftp:foobar.gif is not valid
  156.     if (ftp_available()) {     // ftp supportΘ
  157.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  158.         ok=-1;    // erreur URL
  159.       }
  160.     } else {
  161.       ok=-2;  // non supportΘ
  162.     }
  163. #if HTS_USEMMS
  164.     } else if (strfield(lien,"mms://")) {
  165.         if (ident_url_absolute(lien,adr,fil)==-1) {        
  166.             ok=-1;    // erreur URL
  167.         }
  168. #endif
  169. #if HTS_USEOPENSSL
  170.   } else if (strfield(lien,"https://")) {
  171.     if (SSL_is_available) {
  172.       // Note: ftp:foobar.gif is not valid
  173.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  174.         ok=-1;    // erreur URL
  175.       }
  176.     } else {
  177.       ok=-1;
  178.     }
  179. #endif
  180.   } else if ((scheme) && (
  181.     (!strfield(lien,"http:"))
  182.     && (!strfield(lien,"https:"))
  183.     && (!strfield(lien,"ftp:"))
  184. #if HTS_USEMMS
  185.     && (!strfield(lien,"mms:"))
  186. #endif
  187.     )) {
  188.     ok=-1;      // unknown scheme
  189.   } else {    // c'est un lien relatif
  190.     // On forme l'URL complΦte α partie de l'url actuelle
  191.     // et du chemin actuel si besoin est.
  192.     
  193.     // copier adresse
  194.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  195.  
  196.       /* patch scheme if necessary */
  197.       if (strfield(lien,"http:")) {
  198.         lien+=5;
  199.         strcpybuff(adr, jump_protocol(origin_adr));    // mΩme adresse ; protocole vide (http)
  200.       } else if (strfield(lien,"https:")) {
  201.         lien+=6;
  202.         strcpybuff(adr, "https://");   // mΩme adresse forcΘe en https
  203.         strcatbuff(adr, jump_protocol(origin_adr));
  204.       } else if (strfield(lien,"ftp:")) {
  205.         lien+=4;
  206.         strcpybuff(adr, "ftp://");   // mΩme adresse forcΘe en ftp
  207.         strcatbuff(adr, jump_protocol(origin_adr));
  208. #if HTS_USEMMS
  209.       } else if (strfield(lien,"mms:")) {
  210.         lien+=4;
  211.         strcpybuff(adr, "mms://");   // mΩme adresse forcΘe en ftp
  212.         strcatbuff(adr, jump_protocol(origin_adr));
  213. #endif
  214.       } else {
  215.         strcpybuff(adr,origin_adr);    // mΩme adresse ; et mΩme Θventuel protocole
  216.       }
  217.       
  218.       if (*lien!='/') {  // sinon c'est un lien absolu
  219.         if (*lien == '\0') {
  220.           strcpybuff(fil,origin_fil);
  221.         } else if (*lien == '?') {     // example: a href="?page=2"
  222.           char* a;
  223.           strcpybuff(fil,origin_fil);
  224.           a=strchr(fil,'?');
  225.           if (a) *a='\0';
  226.           strcatbuff(fil,lien);
  227.         } else {
  228.           const char *a=strchr(origin_fil,'?');
  229.           if (a == NULL) a=origin_fil+strlen(origin_fil);
  230.           while((*a!='/') && ( a > origin_fil) ) a--;
  231.           if (*a=='/') {    // ok on a un '/'
  232.             if ( (((int) (a - origin_fil))+1+strlen(lien)) < HTS_URLMAXSIZE) {
  233.               // copier chemin
  234.               strncpy(fil,origin_fil,((int) (a - origin_fil))+1);
  235.               *(fil + ((int) (a - origin_fil))+1)='\0';
  236.               
  237.               // copier chemin relatif
  238.               if (((int) strlen(fil)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  239.                 strcatbuff(fil,lien + ((*lien=='/')?1:0) );      
  240.                 // simplifier url pour les ../
  241.                 fil_simplifie(fil);
  242.               } else
  243.                 ok=-1;    // erreur
  244.             } else {    // erreur
  245.               ok=-1;    // erreur URL
  246.             }
  247.           } else {    // erreur
  248.             ok=-1;    // erreur URL
  249.           }
  250.         }
  251.       } else { // chemin absolu
  252.         // copier chemin directement
  253.         strcatbuff(fil,lien);      
  254.         fil_simplifie(fil);
  255.       }  // *lien!='/'
  256.     } else
  257.       ok=-1;
  258.     
  259.   }  // test news: etc.
  260.  
  261.   // case insensitive pour adresse
  262.   {
  263.     char *a=jump_identification(adr);
  264.     while(*a) {
  265.       if ((*a>='A') && (*a<='Z'))
  266.         *a+='a'-'A';       
  267.       a++;
  268.     }
  269.   }
  270.   
  271.   return ok;
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  279. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  280. int lienrelatif(char* s,const char* link,const char* curr_fil) {
  281.   char BIGSTK _curr[HTS_URLMAXSIZE*2];
  282.   char BIGSTK newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  283.   char* curr;
  284.   //int n=0;
  285.   char* a;
  286.   int slash=0;
  287.   //
  288.   newcurr_fil[0]='\0'; newlink[0]='\0';
  289.   //
  290.  
  291.   // patch: Θliminer les ? (paramΦtres) sinon bug
  292.   {
  293.     const char* a;
  294.     if ( (a=strchr(curr_fil,'?')) ) {
  295.       strncatbuff(newcurr_fil,curr_fil,(int) (a - curr_fil));
  296.       curr_fil = newcurr_fil;
  297.     }
  298.     if ( (a=strchr(link,'?')) ) {
  299.       strncatbuff(newlink,link,(int) (a - link));
  300.       link = newlink;
  301.     }
  302.   }
  303.  
  304.   // recopier uniquement le chemin courant
  305.   curr=_curr;
  306.   strcpybuff(curr,curr_fil);
  307.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  308.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  309.   while((*a!='/') && ( a> curr)) a--;       // chercher dernier / du chemin courant
  310.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  311.   
  312.   // "effacer" s
  313.   s[0]='\0';
  314.   
  315.   // sauter ce qui est commun aux 2 chemins
  316.   {
  317.     const char *l,*c;
  318.     if (*link=='/') link++;  // sauter slash
  319.     if (*curr=='/') curr++;
  320.     l=link;
  321.     c=curr;
  322.     // couper ce qui est commun
  323.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  324.     // mais on veut un rΘpertoirer entier!
  325.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  326.     while(((*link!='/') || (*curr!='/')) && ( link > l)) { link--; curr--; }
  327.     //if (*link=='/') link++;
  328.     //if (*curr=='/') curr++;
  329.   }
  330.   
  331.   // calculer la profondeur du rΘpertoire courant et remonter
  332.   // LES ../ ONT ETE SIMPLIFIES
  333.   a=curr;
  334.   if (*a=='/') a++;
  335.   while(*a) if (*(a++)=='/') strcatbuff(s,"../");
  336.   //if (strlen(s)==0) strcatbuff(s,"/");
  337.  
  338.   if (slash) strcatbuff(s,"/");    // garder absolu!!
  339.   
  340.   // on est dans le rΘpertoire de dΘpart, copier
  341.   strcatbuff(s,link + ((*link=='/')?1:0) );
  342.  
  343.   /* Security check */
  344.   if (strlen(s) >= HTS_URLMAXSIZE)
  345.     return -1;
  346.  
  347.   // on a maintenant une chaine de la forme ../../test/truc.html  
  348.   return 0;
  349. }
  350.  
  351. /* Is the link absolute (http://www..) or relative (/bar/foo.html) ? */
  352. int link_has_authority(const char* lien) {
  353.   const char* a=lien;
  354.   if (isalpha((unsigned char)*a)) {
  355.     // Skip scheme?
  356.     while (isalpha((unsigned char)*a))
  357.       a++;
  358.     if (*a == ':')
  359.       a++;
  360.     else
  361.       return 0;
  362.   }
  363.   if (strncmp(a,"//",2) == 0)
  364.     return 1;
  365.   return 0;
  366. }
  367.  
  368. int link_has_authorization(const char* lien) {
  369.   const char* adr = jump_protocol(lien);
  370.   const char* firstslash = strchr(adr, '/');
  371.   const char* detect = strchr(adr, '@');
  372.   if (firstslash) {
  373.     if (detect) {
  374.       return (detect < firstslash);
  375.     }
  376.   } else {
  377.     return (detect != NULL);
  378.   }
  379.   return 0;
  380. }
  381.  
  382.  
  383. // conversion chemin de fichier/dossier vers 8-3 ou ISO9660
  384. void long_to_83(int mode,char* n83,char* save) {
  385.   n83[0]='\0';
  386.  
  387.   while(*save) {
  388.     char fn83[256],fnl[256];
  389.     int i=0;
  390.     fn83[0]=fnl[0]='\0';
  391.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  392.     fnl[i]='\0';
  393.     // conversion
  394.     longfile_to_83(mode,fn83,fnl);
  395.     strcatbuff(n83,fn83);
  396.  
  397.     save+=i;
  398.     if (*save=='/') { strcatbuff(n83,"/"); save++; }
  399.   }
  400. }
  401.  
  402.  
  403. // conversion nom de fichier/dossier isolΘ vers 8-3 ou ISO9660
  404. void longfile_to_83(int mode,char* n83,char* save) {
  405.   int j=0,max=0;
  406.   int i = 0;
  407.   char nom[256];
  408.   char ext[256];
  409.   nom[0]=ext[0]='\0';
  410.   
  411.   switch(mode) {
  412.   case 1:
  413.     max=8;
  414.     break;
  415.   case 2:
  416.     max=31;
  417.     break;
  418.   default:
  419.     max=8;
  420.     break;
  421.   }
  422.  
  423.   /* No starting . */
  424.   if (save[0] == '.') {
  425.     save[0]='_';
  426.   }
  427.   /* No multiple dots */
  428.   {
  429.     char* last_dot=strrchr(save, '.');
  430.     char* dot;
  431.     while((dot=strchr(save, '.'))) {
  432.       *dot = '_';
  433.     }
  434.     if (last_dot) {
  435.       *last_dot='.';
  436.     }
  437.   }
  438.   /* 
  439.   Avoid: (ISO9660, but also suitable for 8-3)
  440.   (Thanks to jonat@cellcast.com for te hint)
  441.   /:;?\#*~
  442.   0x00-0x1f and 0x80-0xff
  443.   */
  444.   for(i = 0 ; save[i] != 0 ; i++) {
  445.     char a = save[i];
  446.     if (a >= 'a' && a <= 'z') {
  447.       a -= 'a' - 'A';
  448.     }
  449.     else if ( ! ( (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9') || a == '_' || a == '.') ) {
  450.       a = '_';
  451.     }
  452.     save[i] = a;
  453.   }
  454.  
  455.   i=j=0;
  456.   while((i<max) && (save[j]) && (save[j]!='.')) {
  457.     if (save[j]!=' ') {
  458.       nom[i]=save[j]; 
  459.       i++; 
  460.     } 
  461.     j++; 
  462.   }  // recopier nom
  463.   nom[i]='\0';
  464.   if (save[j]) {  // il reste au moins un point
  465.     i = (int) strlen(save)-1;
  466.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  467.     if (save[i]=='.') {  // point!
  468.       int j=0;
  469.       i++;
  470.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  471.       ext[j]='\0';
  472.     }
  473.   }
  474.   // corriger vers 8-3
  475.   n83[0]='\0';
  476.   strncatbuff(n83,nom,max);
  477.   if (strnotempty(ext)) {
  478.     strcatbuff(n83,".");
  479.     strncatbuff(n83,ext,3);    
  480.   }
  481. }
  482.  
  483. // Θcrire backblue.gif
  484. int verif_backblue(httrackp* opt, const char* base) {
  485.   int* done = &opt->state.verif_backblue_done;
  486.   int ret=0;
  487.   //
  488.   if (!base) {   // init
  489.     *done=0;
  490.     return 0;
  491.   }
  492.   if ( (!*done)
  493.     || (fsize(fconcat(OPT_GET_BUFF(opt), base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN)) {
  494.     FILE* fp = filecreate(&opt->state.strc, fconcat(OPT_GET_BUFF(opt), base,"backblue.gif"));
  495.     *done=1;
  496.     if (fp) {
  497.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  498.         ret=1;
  499.       fclose(fp);
  500.       usercommand(opt,0,NULL,fconcat(OPT_GET_BUFF(opt), base,"backblue.gif"),"","");
  501.     } else
  502.       ret=1;
  503.     //
  504.     fp = filecreate(&opt->state.strc, fconcat(OPT_GET_BUFF(opt), base,"fade.gif"));
  505.     if (fp) {
  506.       if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
  507.         ret=1;
  508.       fclose(fp);
  509.       usercommand(opt,0,NULL,fconcat(OPT_GET_BUFF(opt), base,"fade.gif"),"","");
  510.     } else
  511.       ret=1;
  512.   } 
  513.   return ret;
  514. }
  515.  
  516. // flag
  517. int verif_external(httrackp* opt,int nb,int test) {
  518.   int* status = &opt->state.verif_external_status;
  519.   if (!test)
  520.     status[nb]=0;   // reset
  521.   else if (!status[nb]) {
  522.     status[nb]=1;
  523.     return 1;
  524.   }
  525.   return 0;
  526. }
  527.  
  528.  
  529. // recherche chaεne de type truc<espaces>=
  530. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  531. /* SECTION OPTIMISEE:
  532. #define rech_tageq(adr,s) ( \
  533.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  534.     ( (streql(*adr,*s)) ? \
  535.       (__rech_tageq(adr,s)) \
  536.       : 0 \
  537.     ) \
  538.     : 0\
  539.   )
  540. */
  541. /*
  542. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  543.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  544.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  545.       return __rech_tageq(adr,s);
  546.     }
  547.   }
  548.   return 0;
  549. }
  550. */
  551. // DeuxiΦme partie
  552. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  553.   int p;
  554.   p=strfield(adr,s);
  555.   if (p) {
  556.     while(is_space(adr[p])) p++;
  557.     if (adr[p]=='=') {
  558.       return p+1;
  559.     }
  560.   }
  561.   return 0;
  562. }
  563.  
  564. HTS_INLINE int rech_tageq_all(const char* adr, const char* s) { 
  565.     int p;
  566.     char quot = 0;
  567.     const char *token = NULL;
  568.     int s_len = (int) strlen(s);
  569.     if (adr == NULL) {
  570.         return 0;
  571.     }
  572.     for(p = 0 ; adr[p] != 0 ; p++) {
  573.         if (quot == 0) {
  574.             if (adr[p] == '"' || adr[p] == '\'' ) {
  575.                 quot = adr[p];
  576.             } else if (adr[p] == '=' || is_realspace(adr[p]) ) {
  577.                 token = NULL;
  578.             } else if (adr[p] == '>') {
  579.                 break;
  580.             } else {                /* note: bogus for bogus foo = bar */
  581.                 if (token == NULL) {
  582.                     if (strncasecmp(&adr[p], s, s_len) == 0 
  583.                         && (is_realspace(adr[p + s_len]) || adr[p + s_len] == '=')
  584.                         ) {
  585.                         for( p += s_len ; is_realspace(adr[p]) || adr[p] == '=' ; p++ );
  586.                         return p;
  587.                     }
  588.                     token = &adr[p];
  589.                 }
  590.             }
  591.         } else if (adr[p] == quot) {
  592.             quot = 0;
  593.         }
  594.     }
  595.   return 0;
  596. }
  597.  
  598. HTS_INLINE int rech_endtoken(const char* adr, const char** start) {
  599.   char quote = '\0';
  600.   int length = 0;
  601.   while(is_space(*adr)) adr++;
  602.   if (*adr == '"' || *adr == '\'') 
  603.     quote = *adr++;
  604.   *start = adr;
  605.   while(*adr != 0 && *adr != quote && (quote != '\0' || !is_space(*adr)) ) {
  606.     length++;
  607.     adr++;
  608.   }
  609.   return length;
  610. }
  611. // same, but check begining of adr wirh s (for <object src="bar.mov" .. hotspot123="foo.html">)
  612. HTS_INLINE int __rech_tageqbegdigits(const char* adr,const char* s) { 
  613.   int p;
  614.   p=strfield(adr,s);
  615.   if (p) {
  616.     while(isdigit((unsigned char)adr[p]))  p++;      // jump digits
  617.     while(is_space(adr[p])) p++;
  618.     if (adr[p]=='=') {
  619.       return p+1;
  620.     }
  621.   }
  622.   return 0;
  623. }
  624.  
  625. // tag sans =
  626. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  627.   int p;
  628.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  629.     p=strfield(adr,s);
  630.     if (p) {
  631.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  632.         return 1;
  633.       }
  634.       return 0;
  635.     }
  636.   }
  637.   return 0;
  638. }
  639.  
  640. // teste si le tag contenu dans from est Θgal α "tag"
  641. HTS_INLINE int check_tag(char* from,const char* tag) {
  642.   char* a=from+1;
  643.   int i=0;
  644.   char s[256];
  645.   while(is_space(*a)) a++;
  646.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  647.   s[i++]='\0';
  648.   return (strfield2(s,tag));  // comparer
  649. }
  650.  
  651. // teste si un fichier dΘpasse le quota
  652. int istoobig(httrackp *opt,LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  653.   int ok=1;
  654.   if (size>0) {
  655.     if (is_hypertext_mime(opt,type, "")) {
  656.       if (maxhtml>0) {
  657.         if (size>maxhtml)
  658.           ok=0;
  659.       }
  660.     } else {
  661.       if (maxnhtml>0) {
  662.         if (size>maxnhtml)
  663.           ok=0;
  664.       }
  665.     }
  666.   }
  667.   return (!ok);
  668. }
  669.  
  670.  
  671. static int sortTopIndexFnc(const void * a_, const void * b_) {
  672.   int cmp;
  673.   topindex_chain** a = (topindex_chain**) a_;
  674.   topindex_chain** b = (topindex_chain**) b_;
  675.   /* Category first, then name */
  676.   if ((cmp = (*a)->level - (*b)->level) == 0) {
  677.     if ((cmp = strcmpnocase((*a)->category, (*b)->category)) == 0) {
  678.       cmp = strcmpnocase((*a)->name, (*b)->name);
  679.     }
  680.   }
  681.   return cmp;
  682. }
  683.  
  684. HTSEXT_API char* hts_getcategory(const char* filename);
  685.  
  686. HTSEXT_API int hts_buildtopindex(httrackp* opt,const char* path,const char* binpath) {
  687.   FILE* fpo;
  688.   int retval=0;
  689.   char BIGSTK rpath[1024*2];
  690.   char *toptemplate_header=NULL,*toptemplate_body=NULL,*toptemplate_footer=NULL,*toptemplate_bodycat=NULL;
  691.      char catbuff[CATBUFF_SIZE];
  692.  
  693.   // et templates html
  694.   toptemplate_header=readfile_or(fconcat(catbuff, binpath,"templates/topindex-header.html"),HTS_INDEX_HEADER);
  695.   toptemplate_body=readfile_or(fconcat(catbuff, binpath,"templates/topindex-body.html"),HTS_INDEX_BODY);
  696.   toptemplate_bodycat=readfile_or(fconcat(catbuff, binpath,"templates/topindex-bodycat.html"),HTS_INDEX_BODYCAT);
  697.   toptemplate_footer=readfile_or(fconcat(catbuff, binpath,"templates/topindex-footer.html"),HTS_INDEX_FOOTER);
  698.   
  699.   if (toptemplate_header && toptemplate_body && toptemplate_footer && toptemplate_bodycat) {
  700.     
  701.     strcpybuff(rpath,path);
  702.     if (rpath[0]) {
  703.       if (rpath[strlen(rpath)-1]=='/')
  704.         rpath[strlen(rpath)-1]='\0';
  705.     }
  706.     
  707.     fpo=fopen(fconcat(catbuff, rpath,"/index.html"),"wb");
  708.     if (fpo) {
  709.       find_handle h;
  710.       verif_backblue(opt,concat(catbuff, rpath,"/"));    // gΘnΘrer gif
  711.       // Header
  712.       fprintf(fpo,toptemplate_header,
  713.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  714.         );
  715.       
  716.       /* Find valid project names */
  717.       h = hts_findfirst(rpath);
  718.       if (h) {
  719.         struct topindex_chain * chain=NULL;
  720.         struct topindex_chain * startchain=NULL;
  721.         String iname = STRING_EMPTY;
  722.         int chainSize = 0;
  723.         do {
  724.           if (hts_findisdir(h)) {
  725.             StringCopy(iname,rpath);
  726.             StringCat(iname,"/");
  727.             StringCat(iname,hts_findgetname(h));
  728.             StringCat(iname,"/index.html");
  729.             if (fexist(StringBuff(iname))) {
  730.               int level = 0;
  731.               char* category = NULL;
  732.               struct topindex_chain * oldchain=chain;
  733.               
  734.               /* Check for an existing category */
  735.               StringCopy(iname,rpath);
  736.               StringCat(iname,"/");
  737.               StringCat(iname,hts_findgetname(h));
  738.               StringCat(iname,"/hts-cache/winprofile.ini");
  739.               if (fexist(StringBuff(iname))) {
  740.                 category = hts_getcategory(StringBuff(iname));
  741.                 if (category != NULL) {
  742.                   if (*category == '\0') {
  743.                     freet(category);
  744.                     category = NULL;
  745.                   }
  746.                 }
  747.               }
  748.               if (category == NULL) {
  749.                 category = strdupt("No categories");
  750.                 level = 1;
  751.               }
  752.  
  753.               chain=calloc(sizeof(struct topindex_chain), 1);
  754.               chainSize++;
  755.               if (!startchain) {
  756.                 startchain=chain;
  757.               }
  758.               if (chain) {
  759.                 if (oldchain) {
  760.                   oldchain->next=chain;
  761.                 }
  762.                 chain->next=NULL;
  763.                 strcpybuff(chain->name, hts_findgetname(h));
  764.                 chain->category = category;
  765.                 chain->level = level;
  766.               }
  767.             }
  768.             
  769.           }
  770.         } while(hts_findnext(h));
  771.         hts_findclose(h);
  772.         StringFree(iname);
  773.         
  774.         /* Sort chain */
  775.         {
  776.           struct topindex_chain** sortedElts = (struct topindex_chain**) calloct(sizeof(topindex_chain*), chainSize);
  777.           assertf(sortedElts != NULL);
  778.           if (sortedElts != NULL) {
  779.             int i;
  780.             char* category = "";
  781.             
  782.             /* Build array */
  783.             struct topindex_chain * chain = startchain;
  784.             for(i = 0 ; i < chainSize ; i++) {
  785.               assertf(chain != NULL);
  786.               sortedElts[i] = chain;
  787.               chain = chain->next;
  788.             }
  789.             qsort(sortedElts, chainSize, sizeof(topindex_chain*), sortTopIndexFnc);
  790.             
  791.             /* Build sorted index */
  792.             for(i = 0 ; i < chainSize ; i++) {
  793.               char BIGSTK hname[HTS_URLMAXSIZE*2];
  794.               strcpybuff(hname,sortedElts[i]->name);
  795.               escape_check_url(hname);
  796.  
  797.               /* Changed category */
  798.               if (strcmp(category, sortedElts[i]->category) != 0) {
  799.                 category = sortedElts[i]->category;
  800.                 fprintf(fpo,toptemplate_bodycat, category);
  801.               }
  802.               fprintf(fpo,toptemplate_body,
  803.                 hname,
  804.                 sortedElts[i]->name
  805.                 );
  806.             }
  807.             
  808.             /* Wipe elements */
  809.             for(i = 0 ; i < chainSize ; i++) {
  810.               freet(sortedElts[i]->category);
  811.               freet(sortedElts[i]);
  812.               sortedElts[i] = NULL;
  813.             }
  814.             freet(sortedElts);
  815.             
  816.             /* Return value */
  817.             retval=1;
  818.           }
  819.         }
  820.         
  821.       }
  822.       
  823.       // Footer
  824.       fprintf(fpo,toptemplate_footer,
  825.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  826.         );
  827.       
  828.       fclose(fpo);
  829.       
  830.     }
  831.     
  832.   }
  833.  
  834.   if (toptemplate_header)
  835.     freet(toptemplate_header);
  836.   if (toptemplate_body)
  837.     freet(toptemplate_body);
  838.   if (toptemplate_footer)
  839.     freet(toptemplate_footer);
  840.   if (toptemplate_body)
  841.     freet(toptemplate_body);
  842.   
  843.   return retval;
  844. }
  845.  
  846. HTSEXT_API char* hts_getcategory(const char* filename) {
  847.   String categ = STRING_EMPTY;
  848.   if (fexist(filename)) {
  849.     FILE* fp = fopen(filename, "rb");
  850.     if (fp != NULL) {
  851.       int done=0;
  852.       while(!feof(fp) && !done) {
  853.         char BIGSTK line[1024];
  854.         int n = linput(fp, line, sizeof(line) - 2);
  855.         if (n > 0) {
  856.           if (strfield(line, "category=")) {
  857.             unescapehttp(line+9, &categ);
  858.             done=1;
  859.           }
  860.         }
  861.       }
  862.       fclose(fp);
  863.     }
  864.   }
  865.   return StringBuffRW(categ);
  866. }
  867.  
  868. HTSEXT_API char* hts_getcategories(char* path, int type) {
  869.   String categ = STRING_EMPTY;
  870.   String profiles = STRING_EMPTY;
  871.   char* rpath = path;
  872.   find_handle h;
  873.   inthash hashCateg = NULL;
  874.   if (rpath[0]) {
  875.     if (rpath[strlen(rpath)-1]=='/') {
  876.       rpath[strlen(rpath)-1]='\0';      /* note: patching stored (inhash) value */
  877.     }
  878.   }
  879.   h = hts_findfirst(rpath);
  880.   if (h) {
  881.     String iname = STRING_EMPTY;
  882.     if (type == 1) {
  883.       hashCateg = inthash_new(127);
  884.       StringCat(categ, "Test category 1");
  885.       StringCat(categ, "\r\nTest category 2");
  886.     }
  887.     do {
  888.       if (hts_findisdir(h)) {
  889.         char BIGSTK line2[1024];
  890.         StringCopy(iname,rpath);
  891.         StringCat(iname,"/");
  892.         StringCat(iname,hts_findgetname(h));
  893.         StringCat(iname,"/hts-cache/winprofile.ini");
  894.         if (fexist(StringBuff(iname))) {
  895.           if (type == 1) {
  896.             FILE* fp = fopen(StringBuff(iname), "rb");
  897.             if (fp != NULL) {
  898.               int done=0;
  899.               while(!feof(fp) && !done) {
  900.                 int n = linput(fp, line2, sizeof(line2) - 2);
  901.                 if (n > 0) {
  902.                   if (strfield(line2, "category=")) {
  903.                     if (*(line2+9)) {
  904.                       if (!inthash_read(hashCateg, line2+9, NULL)) {
  905.                         inthash_write(hashCateg, line2+9, 0);
  906.                         if (StringLength(categ) > 0) {
  907.                           StringCat(categ, "\r\n");
  908.                         }
  909.                         unescapehttp(line2+9, &categ);
  910.                       }
  911.                     }
  912.                     done=1;
  913.                   }
  914.                 }
  915.               }
  916.               line2[0] = '\0';
  917.               fclose(fp);
  918.             }
  919.           } else {
  920.             if (StringLength(profiles) > 0) {
  921.               StringCat(profiles, "\r\n");
  922.             }
  923.             StringCat(profiles, hts_findgetname(h));
  924.           }
  925.         }
  926.         
  927.       }
  928.     } while(hts_findnext(h));
  929.     hts_findclose(h);
  930.     StringFree(iname);
  931.   }
  932.   if (hashCateg) {
  933.     inthash_delete(&hashCateg);
  934.     hashCateg = NULL;
  935.   }
  936.   if (type == 1)
  937.     return StringBuffRW(categ);
  938.   else
  939.     return StringBuffRW(profiles);
  940. }
  941.  
  942.  
  943.  
  944.  
  945. // Portable directory find functions
  946. /*
  947. // Example:
  948. find_handle h = hts_findfirst("/tmp");
  949. if (h) {
  950.   do {
  951.     if (hts_findisfile(h))
  952.       printf("File: %s (%d octets)\n",hts_findgetname(h),hts_findgetsize(h));
  953.     else if (hts_findisdir(h))
  954.       printf("Dir: %s\n",hts_findgetname(h));
  955.   } while(hts_findnext(h));
  956.   hts_findclose(h);
  957. }
  958. */
  959. HTSEXT_API find_handle hts_findfirst(char* path) {
  960.   if (path) {
  961.     if (strnotempty(path)) {
  962.       find_handle_struct* find = (find_handle_struct*) calloc(1,sizeof(find_handle_struct));
  963.       if (find) {
  964.         memset(find, 0, sizeof(find_handle_struct));
  965. #ifdef _WIN32
  966.         {
  967.           char BIGSTK rpath[1024*2];
  968.           strcpybuff(rpath,path);
  969.           if (rpath[0]) {
  970.             if (rpath[strlen(rpath)-1]!='\\')
  971.               strcatbuff(rpath,"\\");
  972.           }
  973.           strcatbuff(rpath,"*.*");
  974.           find->handle = FindFirstFileA(rpath,&find->hdata);
  975.           if (find->handle != INVALID_HANDLE_VALUE)
  976.             return find;
  977.         }
  978. #else
  979.         strcpybuff(find->path,path);
  980.         {
  981.           if (find->path[0]) {
  982.             if (find->path[strlen(find->path)-1]!='/')
  983.               strcatbuff(find->path,"/");
  984.           }
  985.         }
  986.         find->hdir=opendir(path);
  987.         if (find->hdir != NULL) {
  988.           if (hts_findnext(find) == 1)
  989.             return find;
  990.         }
  991. #endif
  992.         free((void*)find);
  993.       }
  994.     }
  995.   }
  996.   return NULL;   
  997. }
  998.  
  999. HTSEXT_API int hts_findnext(find_handle find) {
  1000.   if (find) {
  1001. #ifdef _WIN32
  1002.     if ( (FindNextFileA(find->handle,&find->hdata)))
  1003.       return 1;
  1004. #else
  1005.         char catbuff[CATBUFF_SIZE];
  1006.     memset(&(find->filestat), 0, sizeof(find->filestat));
  1007.     if ((find->dirp=readdir(find->hdir)))
  1008.       if (find->dirp->d_name)
  1009.         if (!stat(concat(catbuff, find->path,find->dirp->d_name),&find->filestat))
  1010.           return 1;
  1011. #endif
  1012.   }
  1013.   return 0;
  1014. }
  1015.  
  1016. HTSEXT_API int hts_findclose(find_handle find) {
  1017.   if (find) {
  1018. #ifdef _WIN32
  1019.     if (find->handle) {
  1020.       FindClose(find->handle);
  1021.       find->handle=NULL;
  1022.     }
  1023. #else
  1024.     if (find->hdir) {
  1025.       closedir (find->hdir);
  1026.       find->hdir=NULL;
  1027.     }
  1028. #endif
  1029.     free((void*)find);
  1030.   }
  1031.   return 0;
  1032. }
  1033.  
  1034. HTSEXT_API char* hts_findgetname(find_handle find) {
  1035.   if (find) {
  1036. #ifdef _WIN32
  1037.     return find->hdata.cFileName;
  1038. #else
  1039.     if (find->dirp)
  1040.       return find->dirp->d_name;
  1041. #endif
  1042.   }
  1043.   return NULL;
  1044. }
  1045.  
  1046. HTSEXT_API int hts_findgetsize(find_handle find) {
  1047.   if (find) {
  1048. #ifdef _WIN32
  1049.     return find->hdata.nFileSizeLow;
  1050. #else
  1051.     return find->filestat.st_size;
  1052. #endif
  1053.   }
  1054.   return -1;
  1055. }
  1056.  
  1057. HTSEXT_API int hts_findisdir(find_handle find) {
  1058.   if (find) {
  1059.     if (!hts_findissystem(find)) {
  1060. #ifdef _WIN32
  1061.       if (find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
  1062.         return 1;
  1063. #else
  1064.       if (S_ISDIR(find->filestat.st_mode))
  1065.         return 1;
  1066. #endif
  1067.     }
  1068.   }
  1069.   return 0;
  1070. }
  1071. HTSEXT_API int hts_findisfile(find_handle find) {
  1072.   if (find) {
  1073.     if (!hts_findissystem(find)) {
  1074. #ifdef _WIN32
  1075.       if (!(find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY))
  1076.         return 1;
  1077. #else
  1078.       if (S_ISREG(find->filestat.st_mode))
  1079.         return 1;
  1080. #endif
  1081.     }
  1082.   }
  1083.   return 0;
  1084. }
  1085. HTSEXT_API int hts_findissystem(find_handle find) {
  1086.   if (find) {
  1087. #ifdef _WIN32
  1088.     if (find->hdata.dwFileAttributes  & (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_TEMPORARY))
  1089.       return 1;
  1090.     else if ( (!strcmp(find->hdata.cFileName,"..")) || (!strcmp(find->hdata.cFileName,".")) )
  1091.       return 1;
  1092. #else
  1093.     if (
  1094.       (S_ISCHR(find->filestat.st_mode))
  1095.       || 
  1096.       (S_ISBLK(find->filestat.st_mode))
  1097.       || 
  1098.       (S_ISFIFO(find->filestat.st_mode))
  1099.       || 
  1100.       (S_ISSOCK(find->filestat.st_mode))
  1101.       )
  1102.       return 1;
  1103.     else if ( (!strcmp(find->dirp->d_name,"..")) || (!strcmp(find->dirp->d_name,".")) )
  1104.       return 1;
  1105. #endif
  1106.   }
  1107.   return 0;
  1108. }
  1109.